package sort;

/**
 * @author yunfei.shang
 * @title
 * @date
 * @descreption 冒泡排序
 */
public class BubbleSort {
    public static void main(String[] args) {
        int[] baseArray = {10, 1, 8, 6, 4, 6, 3, 5, 9};
        bubbleSortOne(baseArray);
        bubbleSortTwo(baseArray);
    }

    /**
     * 正比排序
     *
     * @param baseArray
     */
    public static void bubbleSortOne(int[] baseArray) {
        int temp;
        for (int i = 0; i < baseArray.length - 1; i++) {
            for (int j = i + 1; j < baseArray.length; j++) {
                if (baseArray[i] > baseArray[j]) {
                    temp = baseArray[i];
                    baseArray[i] = baseArray[j];
                    baseArray[j] = temp;
                }
            }
        }
        for (int i : baseArray) {
            System.out.println(i);
        }
    }

    /**
     * 尾比排序
     *
     * @param baseArray
     */
    public static void bubbleSortTwo(int[] baseArray) {
        int temp;
        for (int i = baseArray.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (baseArray[i] < baseArray[j]) {
                    temp = baseArray[i];
                    baseArray[i] = baseArray[j];
                    baseArray[j] = temp;
                }
            }
        }
        for (int i : baseArray) {
            System.out.println(i);
        }
    }
}
